package com.hdu.dwh.service.impl;

import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdu.dwh.mapper.TableRelationMapper;
import com.hdu.dwh.pojos.source_access_pojos.DataWarehouseTable;
import com.hdu.dwh.pojos.source_access_pojos.TableRelation;
import com.hdu.dwh.service.DataWarehouseTableService;
import com.hdu.dwh.service.TableRelationService;

@Service
public class TableRelationServiceImpl extends ServiceImpl<TableRelationMapper, TableRelation> implements TableRelationService {    
    @Autowired
    private DataWarehouseTableService dataWarehouseTableService;
    @Override
    public TableRelation saveWithSourceTableIdAndTargetTableId(Long sourceId, Long targetId) {
        DataWarehouseTable sourceTable = dataWarehouseTableService.getById(sourceId);
        DataWarehouseTable targetTable = dataWarehouseTableService.getById(targetId);
        TableRelation relation = new TableRelation(sourceId, sourceTable.getTableName(), targetId, targetTable.getTableName());
        this.save(relation);
        return this.getById(relation.getId());
    }

    @Override
    public Set<TableRelation> getSetRelationBySourceTableId(Long sourceTableId) {
        return this.lambdaQuery().eq(TableRelation::getSourceTableId, sourceTableId).list().stream().collect(Collectors.toSet());
    }

}
